package com.hemerasolutions.v2trainer.model.modality;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.exercise.VersionableExercise;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.model.unit.volume.VolumeType;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.model.exercise.executions.Execution;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityModTable;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

import com.hemerasolutions.v2trainer.model.interfaces.BaseReferencedModelInterface;
import com.hemerasolutions.v2trainer.util.annotations.ReferencedModel;


@Entity
public class Modality  extends BaseModel implements BaseReferencedModelInterface {
 
	private static final long serialVersionUID = 1L;
	
	private static String nameKey = "Modality";

    @Column(length = 50)
    private String name;

    @Column(length = 180)
	private String description;

    @Column
	private int version;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date createdAt;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date lastChange;
   
	@ManyToOne 
    private User changedBy;
	
	@OneToOne 
	private Photo icon;
	
	@ManyToOne
	private VersionableModality versionableModality;
	
	@ManyToOne
	private TrainingType trainingType;

    @OneToMany(mappedBy="modality", cascade=CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval=true)
    private List<IntensityModTable> intensityModTables;
	
    @OneToMany(mappedBy="modality", cascade=CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval=true)
	private List<VersionableExercise> versionableExercises;
	 
    @ManyToMany
	private List<Execution> executions;

    @ManyToMany
    private List<VolumeType> volumeTypes;

    @OneToMany(mappedBy="modality",fetch=FetchType.LAZY,orphanRemoval=true)
    @ReferencedModel(compareOnChange = false)
    private List<ExerciseGroup> referencedExerciseGroups;
    
    @ManyToMany(fetch=FetchType.LAZY)
    @ReferencedModel(compareOnChange = false)
    private List<Training> referencedTrainings;
    
    
    public void fillReferences(){
    	// No reference to change
    }
    
    
	public Modality() {
		super();
		this.referencedExerciseGroups = new ArrayList<ExerciseGroup>();
		this.referencedTrainings = new ArrayList<Training>();
		
		this.executions = new ArrayList<Execution>();
		this.volumeTypes = new ArrayList<VolumeType>();
		this.versionableExercises = new ArrayList<VersionableExercise>(); 
		this.intensityModTables = new ArrayList<IntensityModTable>();
	}

	
	public Modality(Modality other,VersionableModality versionableModality){
		this();

		this.versionableModality = versionableModality;
		this.changedBy = other.changedBy;
		this.createdAt = other.createdAt;
		this.description = other.description;
		this.lastChange = other.lastChange;
		this.name = other.name;
		this.trainingType = other.trainingType;
		this.icon = other.icon;
		this.executions.addAll(other.executions);
		this.volumeTypes.addAll(other.volumeTypes);
		
		for(IntensityModTable imt:other.intensityModTables){
			this.intensityModTables.add(new IntensityModTable(imt,this));
		}
		
		for( VersionableExercise ve: other.versionableExercises){
			this.versionableExercises.add(new VersionableExercise(ve,this));
		}

	}
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public Date getLastChange() {
		return lastChange;
	}

	public User getChangedBy() {
		return changedBy;
	}

	public TrainingType getTrainingType() {
		return trainingType;
	}

	public List<VersionableExercise> getVersionableExercises() {
		return versionableExercises;
	}

	public List<Execution> getExecutions() {
		return executions;
	}


	public List<VolumeType> getVolumeTypes() {
		return volumeTypes;
	}

	public void setVolumeTypes(List<VolumeType> volumeTypes) {
		this.volumeTypes = volumeTypes;
	}

	public Photo getIcon() {
		return icon;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public void setLastChange(Date lastChange) {
		this.lastChange = lastChange;
	}

	public void setChangedBy(User changedBy) {
		this.changedBy = changedBy;
	}

	public void setTrainingType(TrainingType trainingType) {
		this.trainingType = trainingType;
	}

	public void setVersionableExercises(List<VersionableExercise> versionableExercises) {
		this.versionableExercises = versionableExercises;
	}

	public void setExecutions(List<Execution> executions) {
		this.executions = executions;
	}

	public void setIcon(Photo icon) {
		this.icon = icon;
	}

	
	public List<IntensityModTable> getIntensityModTables() {
		return intensityModTables;
	}

	public void setIntensityModTables(List<IntensityModTable> intensityModalitys) {
		this.intensityModTables = intensityModalitys;
	}

	public VersionableModality getVersionableModality() {
		return versionableModality;
	}

	public void setVersionableModality(VersionableModality versionableModality) {
		this.versionableModality = versionableModality;
	}


	public int getVersion() {
		return version;
	}


	public void setVersion(int version) {
		this.version = version;
	}

	public List<ExerciseGroup> getReferencedExerciseGroups() {
		return referencedExerciseGroups;
	}

	public void setReferencedExerciseGroups(List<ExerciseGroup> usedByExerciseGroups) {
		this.referencedExerciseGroups = usedByExerciseGroups;
	}


	public List<Training> getReferencedTrainings() {
		return referencedTrainings;
	}


	public void setReferencedTrainings(List<Training> usebByTrainings) {
		this.referencedTrainings = usebByTrainings;
	}

	public String getNameKey() {
		return nameKey;
	}

	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((changedBy == null) ? 0 : changedBy.hashCode());
		result = prime * result
				+ ((createdAt == null) ? 0 : createdAt.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((icon == null) ? 0 : icon.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((lastChange == null) ? 0 : lastChange.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((trainingType == null) ? 0 : trainingType.hashCode());
		result = prime * result + version;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Modality other = (Modality) obj;
		if (changedBy == null) {
			if (other.changedBy != null) {
				return false;
			}
		} else if (!changedBy.equals(other.changedBy)) {
			return false;
		}
		if (createdAt == null) {
			if (other.createdAt != null) {
				return false;
			}
		} else if (!createdAt.equals(other.createdAt)) {
			return false;
		}
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (icon == null) {
			if (other.icon != null) {
				return false;
			}
		} else if (!icon.equals(other.icon)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (lastChange == null) {
			if (other.lastChange != null) {
				return false;
			}
		} else if (!lastChange.equals(other.lastChange)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (trainingType == null) {
			if (other.trainingType != null) {
				return false;
			}
		} else if (!trainingType.equals(other.trainingType)) {
			return false;
		}
		if (version != other.version) {
			return false;
		}
		return true;
	}


	@Override
	public String toString() {
		return "Modality [id=" + id + ", name=" + name + ", description="
				+ description + ", version=" + version + ", createdAt="
				+ createdAt + ", lastChange=" + lastChange + ", changedBy="
				+ changedBy + ", icon=" + icon + ", trainingType="
				+ trainingType + "]";
	}


	

	
}
 
